//
//  NSString+language.m
//  ColorPickerDemo
//
//  Created by laomi on 2020/7/7.
//  Copyright © 2020 laomi. All rights reserved.
//

#import "NSString+language.h"

extern NSString * const CP_Last_Selected_Language_Identifier;
@implementation NSString (language)
-(NSString*)stringForLanguage:(NSString*)languageIdentifier;
{

    
    NSString *tableName = @"CPMainStringTable";
    NSBundle *selfBundle = [NSBundle mainBundle];
    NSString *str;
    // 没有特殊要求；跟着系统走；
    if(languageIdentifier==nil){
        // iOS 获取设备当前语言和地区的代码
        NSString *currentLanguageRegion = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] firstObject];
        if([currentLanguageRegion hasPrefix:@"zh"]){
            languageIdentifier = @"zh-Hans";
        }else{
            languageIdentifier = @"en";
        }
    }
    NSString *path = [selfBundle pathForResource:languageIdentifier ofType:@"lproj"];
    NSBundle *bundle = [NSBundle bundleWithPath:path];
    
    str =  [bundle localizedStringForKey:self value:@"" table:tableName];
    str = str?str:self;
    return str;
}
+(NSString*)laomi_languageIdentifier;
{
    return  [[NSUserDefaults standardUserDefaults] objectForKey:CP_Last_Selected_Language_Identifier];
}
@end
